/**
 * Created by kevin on 2017/01/08.
 *
 * page base
 */


var eosi = window.eosi || {};
this.eosi.website = this.eosi.website || {};

(function (window) {
    // the application


    function BaseLoader(config) {

        this.Container_constructor();
        this.config = config;
        //console.log(this.config)
        this.init();
        //console.log(this.father.targetWidth);

    }


    var p = createjs.extend(BaseLoader, createjs.Container);
    p.className = "BaseLoader";

    p.config;
    p.loader;
    p.resItems=[];
    p.end_fun=null;
    p.pre_fun=null;

    p.list_first=[];//第一个加载的数组
    p.list_scred=[];//第二个加载的数组

    // public methods:
    p.init = function () {
        this.initData();
        this.loadRes();
    }

    /*
     * 资源分配
     * */
    p.initData=function(){
        for (var i = 0; i < Pagejson.manifest.length; i++) {//循环所有
            if (Pagejson.manifest[i].first_load) {//筛选必选
                this.list_first.push(Pagejson.manifest[i]);
            }else{
        //         this.list_scred.push(Pagejson.manifest[i]);
        //                 console.log(this.list_first)
        // console.log(this.list_scred)
        //         return;
                //每次进来为0
                tj=0;
                //二次筛选 当前页面(可能不是进首页)
                for (var j in Pagejson.manifest[i]["pageName"]) {
                    //当前页面资源
                    if (j==cur_page) {
                        //匹配指数+1
                        tj=1;
                    }
                }

                //如果循环中有一个匹配
                if (tj==1) {
                    //添加进当前页面
                    this.list_first.push(Pagejson.manifest[i]);
                }else{
                    //如果不匹配匹配就添加到第二批
                    this.list_scred.push(Pagejson.manifest[i]);
                }
               
            }
        }
        // console.log(this.list_first)
        // console.log(this.list_scred)
    }

    /*
     * 加载资源
     * */
    p.loadRes = function(){
        this.loader = new createjs.LoadQueue(true);
        this.loader.on("fileload", this.onLoaded, this);
        this.end_fun=this.loader.on("complete", this.loadComplete, this);
        this.pre_fun=this.loader.on("progress", this.onProgress, this);
        this.loader.loadManifest(this.list_first, true, "./");
        // this.loader.loadManifest({src: BASE_PATH + "static/pages/PagesManifest.json?v="+version, callback: "loadPagesMediaGrid", type: "manifest"}, true, "./");
    }
    /*
     * 资源加载
     * */
    p.onLoaded = function (evt){
        var item = evt.item;
        this.resItems.push(item);
    }
    p.onProgress = function(evt){
        if(Video.USE_VIDEO){
            if(Video.firstpage_video){
            }else{
                onProgress(evt,(CORE_RES_SIZE_PER+Video_RES_SIZE_PER),1-(CORE_RES_SIZE_PER+Video_RES_SIZE_PER));
            }
        }else{
            onProgress(evt,(CORE_RES_SIZE_PER+Video_RES_SIZE_PER),1-(CORE_RES_SIZE_PER+Video_RES_SIZE_PER));
        }
    }
    /*
     * 资源加载完毕
     * */
    p.loadComplete = function (evt){
        // console.log(evt);
        this.pageConfigs = pages;
        for(var i = 0;i<this.pageConfigs.length;i++){
            //console.log(this.pageConfigs[i].name ,cur_page);
            if(this.pageConfigs[i].name == cur_page){
                //console.log(cur_page,i);
                contentLayer.createPage(i,true);
                //实例当前页面
                // this.createPage(i,true);
                //this.lastPage = curPage;
            }
        }
        onComplete(evt);
        // this.end_fun.off("complete")
        // this.loader.off("fileload", this.onLoaded, this);
        // this.loader.off("complete", this.loadComplete);
        // this.loader.off("progress", this.onProgress, this);
        // contentLayer.preload(this.pageIndex);
        // alert(1);
        this.loader.off("complete",this.end_fun);
        this.loader.off("progress",this.pre_fun);
        this.loader.loadManifest(this.list_scred, true, "./");
        loadingLayer.visible=false;
        // this.loader.loadManifest({src: BASE_PATH + "static/pages/PagesManifest2.json?v="+version, callback: "loadPagesMediaGrid2", type: "manifest"}, true, "./");
        // document.getElementById("pop").innerHTML=dm;
    }


    // add MyApp to myNameSpace
    window.eosi.website.BaseLoader = createjs.promote(BaseLoader, "Container");
//}());
}(window));





